using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.Scripting;
using TMPro;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine.EventSystems;

[Preserve]
public class UIGenericMethods : NormalSingleton<UIGenericMethods>
{
    public static bool IsIOSRechargeOpen = false;
    public static bool IsAndroidRechargeOpen = false;
    public static bool IsDirectGetAward = false;

    public static float CalculateScale()
    {
        float width = Screen.width;
        float height = Screen.height;
        return (1080f / 2400) / ((width / height));
    }

    public static void FixedScrollRect(RectTransform rect, float baseHeight)
    {
        rect.sizeDelta = new Vector2(rect.rect.width, baseHeight + ((Screen.height * (1080.0f / Screen.width) - 1920)));
    }

    public static string ReturnChinese(int n)
    {
        return n switch
        {
            1 => "一",
            2 => "二",
            3 => "三",
            4 => "四",
            5 => "五",
            6 => "六",
            7 => "七",
            8 => "八",
            9 => "九",
            10 => "十",
            11 => "十一",
            12 => "十二",
            13 => "十三",
            14 => "十四",
            15 => "十五",
            16 => "十六",
            17 => "十七",
            18 => "十八",
            19 => "十九",
            20 => "二十",
            21 => "二十一",
            22 => "二十二",
            23 => "二十三",
            24 => "二十四",
            25 => "二十五",
            26 => "二十六",
            27 => "二十七",
            28 => "二十八",
            29 => "二十九",
            30 => "三十",
            31 => "三十一",
            32 => "三十二",
            33 => "三十三",
            34 => "三十四",
            35 => "三十五",
            36 => "三十六",
            37 => "三十七",
            38 => "三十八",
            39 => "三十九",
            40 => "四十",
            41 => "四十一",
            42 => "四十二",
            43 => "四十三",
            44 => "四十四",
            45 => "四十五",
            46 => "四十六",
            47 => "四十七",
            48 => "四十八",
            49 => "四十九",
            50 => "五十",
            51 => "五十一",
            52 => "五十二",
            53 => "五十三",
            54 => "五十四",
            55 => "五十五",
            56 => "五十六",
            57 => "五十七",
            58 => "五十八",
            59 => "五十九",
            60 => "六十",
            61 => "六十一",
            62 => "六十二",
            63 => "六十三",
            64 => "六十四",
            65 => "六十五",
            66 => "六十六",
            67 => "六十七",
            68 => "六十八",
            69 => "六十九",
            70 => "七十",
            71 => "七十一",
            72 => "七十二",
            73 => "七十三",
            74 => "七十四",
            75 => "七十五",
            76 => "七十六",
            77 => "七十七",
            78 => "七十八",
            79 => "七十九",
            80 => "八十",
            81 => "八十一",
            _ => ""
        };
    }

    public static void ChangeUIState(CanvasGroup group, bool isState, Action action = null)
    {
        if (group != null)
        {
            if (isState)
            {
                group.alpha = 1;
                group.blocksRaycasts = true;
                action?.Invoke();
            }
            else
            {
                group.alpha = 0;
                group.blocksRaycasts = false;
                action?.Invoke();
            }
        }
        else
        {
            Debug.LogError($"请检查选中的窗口:{group.name}");
        }
    }

    public static string ReturnValueText(long value)
    {
        string str = "";
        if (value >= 100000000)
        {
            str = value < 1000000000 ? ((float)value / 100000000f).ToString(".000") + "亿" : ((float)value / 100000000f).ToString(".00") + "亿";
        }
        else if (value >= 100000)
        {
            str = value >= 10000000 ? ((float)value / 10000).ToString("0") + "万" : value >= 1000000 ? str = ((float)value / 10000).ToString(".0") + "万" :
                ((float)value / 10000).ToString(".00") + "万";

            if (value % 10000 == 0)
            {
                str = value / 10000 + "万";
            }
        }
        else
        {
            str = value.ToString();
        }

        return str;
    }

    public static void ChangeIamge(Image image, Sprite sprite)
    {
        image.sprite = sprite;
        image.SetNativeSize();
    }

    /// <summary>
    /// 用于处理替换string描述,中括号中间的字采用别的颜色的方法
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string ProcessString(string str)
    {
        str = str.Replace("[", "<color=green>");
        str = str.Replace("]", "</color>");
        //Debug.Log($"替换后的字符串为{str}");
        return str;
    }

    public static void UpdateCoolDown(TMP_Text coolDown, uint time, int validity)
    {
        DateTime now = DateTime.Now;
        DateTime unlockTime = DateTimeOffset.FromUnixTimeSeconds(time).LocalDateTime + new TimeSpan(validity, 0, 0);

        TimeSpan timeRemaining = unlockTime - now;

        // 格式化时间剩余部分为小时:分钟:秒
        string timeRemainingStr = string.Format("{0}:{1:D2}:{2:D2}",
            timeRemaining.Hours,
            timeRemaining.Minutes,
            timeRemaining.Seconds);
        coolDown.text = timeRemainingStr;
    }


    /// <summary>
    /// Wx小程序判断是否为IOS设备
    /// </summary>
    /// <returns></returns>
    public static bool IsShowRecharge()
    {

        return true;

        //var res = WX.GetDeviceInfo();
        //Debug.Log($"res.platform:{res.platform}");
        //if(res.platform == "Android" || res.platform == "android")
        //{
        //    Debug.Log("为安卓平台");
        //    return IsAndroidRechargeOpen;
        //}
        //else if(res.platform == "Ios" || res.platform == "ios")
        //{
        //    Debug.Log("是IOS平台");
        //    return IsIOSRechargeOpen;
        //}
        //else if(res.platform == "windows" || res.platform == "Windows")
        //{
        //    Debug.Log("是PC平台");
        //    return true;
        //}
        //else if (res.platform == "devtools")
        //{
        //    return true;
        //}
        //else
        //{
        //    Debug.Log("是未知平台");
        //    return true;
        //}

    }

    /// <summary>
    /// 检查是否在该界面
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static bool CheckCanvas(GameObject obj)
    {
        bool active = false;
        if (obj != null)
        {
            var group = obj.GetComponent<CanvasGroup>();
            active = group.alpha == 1;
        }

        return active;
    }

    public static void CanvasPopUp(Transform trans, float scale = .8f)
    {
        trans.localScale = Vector3.zero;
        trans.DOScale(scale, .3f).SetEase(Ease.InOutSine);
    }

    public static void ChangeZ(Transform trans, int z = 0)
    {
        var pos = trans.localPosition;
        trans.localPosition = new(pos.x, pos.y, z);
    }

    /// <summary>
    /// 检测是否点击UI
    /// </summary>
    /// <param name="mousePosition"></param>
    /// <returns></returns>
    public static bool IsPointerOverGameObject(Vector2 mousePosition)
    {
        //创建一个点击事件
        PointerEventData eventData = new PointerEventData(EventSystem.current);
        eventData.position = mousePosition;
        List<RaycastResult> raycastResults = new List<RaycastResult>();
        //向点击位置发射一条射线，检测是否点击UI
        EventSystem.current.RaycastAll(eventData, raycastResults);
        if (raycastResults.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

